<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入资源名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属科目" prop="subjectId">
        <treeselect v-model="queryParams.subjectId" :options="subjectOptions" :normalizer="normalizer" placeholder="请选择科目" style="width:240px"/>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
              <el-option
                v-for="dict in resTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="studyResList">
      <el-table-column label="课程名称" align="center" prop="title" />
      <el-table-column label="所属科目" align="center" prop="subjectName" />
      <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="来源" align="center" prop="source">
        <template slot-scope="scope">
          <a :href="scope.row.source" target="_blank" v-if="scope.row.source">打开</a>
        </template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="fileId">
        <template slot-scope="scope">
          <span v-html="getDownLink(scope.row)"></span>
        </template>
      </el-table-column>
      <el-table-column label="是否推荐" align="center" prop="isRecommend" :formatter="isRecommendFormat" />
      <el-table-column label="浏览次数" align="center" prop="viewCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >学习</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看资源对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <div v-if="form.type == 'v' ">
          <video :source="process.env.VUE_APP_BASE_API+'/'+form.file.path"></video>
      </div>
      <div v-if="form.type == 'a' ">
          <audio :source="process.env.VUE_APP_BASE_API+'/'+form.file.path"></audio>
      </div>
      <div v-if="form.type == 'p' ">
          <img width="760" :src="process.env.VUE_APP_BASE_API+'/'+form.file.path" />
      </div>
      <div v-if="form.type == 'l' ">
          外部资源：<a :href="form.source" target="_blank">点击打开</a>
      </div>
      <div v-if="form.type == 'd' ">
          文档请下载后查看：<span v-html="getDownLink(form)"></span>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStudyRes, getStudyRes, incResView} from "@/api/study/studyRes";
import { listStudySubject} from "@/api/study/studySubject";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "MyStudyRes",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源表格数据
      studyResList: [],
      // 科目树选项
      subjectOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否推荐字典
      isRecommendOptions: [],
      // 资源类型字典
      resTypeOptions: [],
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        subjectId: null,
        type: null,
        source: null,
        isRecommend: null,
        status: '1',
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getDicts("sys_yes_no").then(response => {
      this.isRecommendOptions = response.data;
    });
    this.getDicts("res_type").then(response => {
      this.resTypeOptions = response.data;
    });
    this.getDicts("common_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询资源列表 */
    getList() {
      this.loading = true;
      listStudyRes(this.queryParams).then(response => {
        this.studyResList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 转换科目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.title,
        children: node.children
      };
    },
    /** 查询科目下拉树结构 */
    getTreeselect() {
      listStudySubject().then(response => {
        this.subjectOptions = [];
        const data = { id: 0, title: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "pid");
        this.subjectOptions.push(data);
      });
    },
    // 是否推荐字典翻译
    isRecommendFormat(row, column) {
      return this.selectDictLabel(this.isRecommendOptions, row.isRecommend);
    },
    // 分类字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.resTypeOptions, row.type);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 下载附件链接 */
    getDownLink(row) {
      if(row.file && row.file.id){
        return '<a href="'+process.env.VUE_APP_BASE_API+'/'+row.file.path+'" target="_blank">'+row.file.title+'</a>';
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 学习按钮操作 */
    handleView(row) {
      this.form = row;
      this.open = true;
      this.title = row.title;
      incResView(row.id).then(response => {
        //todo
      });
    }
  }
};
</script>
